package com.dup.jxlh.admin.controller;

import com.dup.jxlh.admin.entities.auth.AuthUser;
import com.dup.jxlh.admin.entities.model.Contract;
import com.dup.jxlh.admin.entities.model.ContractAttach;
import com.dup.jxlh.admin.service.ContractService;
import com.dup.jxlh.admin.validator.ContractId;
import com.dup.jxlh.api.entities.CommonResult;
import com.dup.jxlh.api.entities.PaginationData;
import com.dup.jxlh.api.enums.ResultEnum;
import com.dup.jxlh.api.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigInteger;

@RestController
@RequestMapping("/contract")
@Validated
public class ContractController {
    @Autowired
    private ContractService contractService;

    @PostMapping("/getContractList")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_Contract_Show')")
    public CommonResult getContractList(PaginationData paginationData, @Validated Contract contract) {
        return ResultUtil.result(ResultEnum.SUCCESS, contractService.getContractList(paginationData, contract));
    }

    @PostMapping("/getContractData")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_Contract_Operate')")
    public CommonResult getContractData(PaginationData paginationData, @Validated Contract contract) {
        return ResultUtil.result(ResultEnum.SUCCESS, contractService.getContractData(paginationData, contract));
    }

    @PostMapping("/addContract")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_Contract_Operate')")
    public CommonResult addContract(@Validated Contract contract) {
        if (contractService.addContract(contract)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

    @PostMapping("/editContract")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_Contract_Operate')")
    public CommonResult editContract(@AuthenticationPrincipal AuthUser authUser, @Validated Contract contract) {
        contract.setCompanyId(authUser.getCompanyId());
        if (contractService.editContract(contract)) {
            return ResultUtil.result(ResultEnum.SUCCESS);
        }
        return ResultUtil.result(ResultEnum.ERROR);
    }

    @PostMapping("/addContractAttach")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_Contract_Operate')")
    public CommonResult addContractAttach(@Validated ContractAttach contractAttach) {
        return ResultUtil.result(ResultEnum.SUCCESS, contractService.addContractAttach(contractAttach));
    }

    @PostMapping("/delContractAttach")
    @PreAuthorize("hasAnyAuthority('ROLE_Admin','ROLE_Contract_Operate')")
    public CommonResult delContractAttach(@RequestParam("attachId") BigInteger attachId,
                                          @RequestParam("contractId") @ContractId BigInteger contractId) {
        return ResultUtil.result(ResultEnum.SUCCESS, contractService.delContractAttach(attachId, contractId));
    }
}
